package com.dgut.staffmodule.web;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dgut.common.util.ExtAjaxResponse;
import com.dgut.common.util.ExtJsonResult;
import com.dgut.common.util.ExtPageable;
import com.dgut.staffmodule.entity.Department;
import com.dgut.staffmodule.entity.Position;
import com.dgut.staffmodule.entity.PositionEditDTO;
import com.dgut.staffmodule.entity.PositionQueryDTO;
import com.dgut.staffmodule.service.IDepartmentService;
import com.dgut.staffmodule.service.IPositionService;

@Controller
@RequestMapping("/position")
public class PositionController {

	@Autowired
	private IPositionService positionService;// 职位service
	@Autowired
	private IDepartmentService departmentService;// 部门service

	/**
	 * 保存部门信息
	 *
	 * @param position
	 *            职位信息
	 */
	@RequestMapping("/saveOrUpdate")
	public @ResponseBody ExtAjaxResponse saveOrUpdate(Position position) {
		try {
			positionService.saveOrUpdate(position);
			return new ExtAjaxResponse(true, "操作成功！");
		} catch (Exception e) {
			return new ExtAjaxResponse(false, "操作失败！");
		}
	}

	/**
	 * 保存部门信息
	 *
	 * @param positionEditDTO
	 *            职位信息
	 */
	@RequestMapping("/save")
	public @ResponseBody ExtAjaxResponse save(PositionEditDTO positionEditDTO) {
		try {
			positionService.save(positionEditDTO);
			return new ExtAjaxResponse(true, "操作成功！");
		} catch (Exception e) {
			return new ExtAjaxResponse(false, "操作失败！");
		}
	}

	/**
	 * 保存更改信息
	 *
	 * @param positionEditDTO
	 *            职位信息
	 */
	@RequestMapping("/update")
	public @ResponseBody ExtAjaxResponse update(PositionEditDTO positionEditDTO) {
		try {
			positionService.update(positionEditDTO);
			return new ExtAjaxResponse(true, "操作成功！");
		} catch (Exception e) {
			return new ExtAjaxResponse(false, "操作失败！");
		}
	}

	/**
	 * 删除一个职位
	 *
	 * @param id
	 *            前端传的职位id
	 */
	@RequestMapping("/delete")
	public @ResponseBody ExtAjaxResponse delete(@RequestParam Long id) {
		Position position = positionService.findOne(id);
		boolean flag = positionService.delete(position);
		System.out.println(flag);
		if (flag) {
			return new ExtAjaxResponse(true, "操作成功！");
		} else {
			return new ExtAjaxResponse(false, "删除失败，该职位存在其他员工!");
		}
	}

	/**
	 * 批量删除职位
	 *
	 * @param ids
	 *            前端传的职位ids
	 */
	@RequestMapping("/deletePositions")
	public @ResponseBody ExtAjaxResponse deletePositions(@RequestParam Long[] ids) {

		List<Long> idLists = Arrays.asList(ids);
		boolean flag = positionService.delete(idLists);
		if (flag) {
			return new ExtAjaxResponse(true, "操作成功！");
		} else {
			return new ExtAjaxResponse(false, "删除失败，该职位存在其他员工!");
		}
	}

	/**
	 * 查找一个职位
	 *
	 * @param id
	 *            前端传的职位id
	 */
	@RequestMapping("/findOne")
	public @ResponseBody Position findOne(@RequestParam Long id) {
		Position position = positionService.findOne(id);

		return position;
	}

	/**
	 * 查找所有职位
	 */
	@RequestMapping("/findAll")
	public @ResponseBody ExtJsonResult<Position> findAll() {
		List<Position> lists = positionService.findAll();
		return new ExtJsonResult<Position>(lists);
	}

	/**
	 * 分页查找职位
	 *
	 * @param positionQueryDTO
	 *            前端传的部门信息positionQueryDTO
	 * @param pageable
	 *            分页条件
	 */
	@RequestMapping("/findPage")
	public @ResponseBody Page<Position> findPage(PositionQueryDTO positionQueryDTO, ExtPageable pageable) {
		Page<Position> page = positionService.findAll(PositionQueryDTO.getSpecification(positionQueryDTO),
				pageable.getPageable());
		return page;
	}

	/**
	 * 查找一个职位
	 *
	 * @param deptName
	 *            通过部门id查找出其下职位
	 */
	@RequestMapping("/findAllByDeptId")
	public @ResponseBody ExtJsonResult<Position> findAllByDeptId(@RequestParam String deptName) {
		Department department = departmentService.findByDeptName(deptName);
		List<Position> lists = positionService.findAllByDeptId(department.getId());
		for (Position p : lists) {
			System.out.println(p);
		}
		return new ExtJsonResult<Position>(lists);
	}
}
